package com.dap.test.app;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.dap.test.DisconfTestConstant;

/**
 * 获取环境信息
 * @author wshh-dengxiang
 *
 */
public class GetAppById {
	
	private static String sessionId;
	private static String appId;
	private static String appName="appGetTest_20160412"+Math.random();
	private static String desc="appGetTest_20160412"+Math.random();
	
	@Before
	public void before(){
		Client client = ClientBuilder.newClient();
		StringBuffer url = new StringBuffer();
		url.append(DisconfTestConstant.CONSOLE_REST_URI_PREFIX+"/services/sys/login?loginName=admin&password=admin");
		WebTarget target = client.target(url.toString());
		Response response = target.request().get();
		String str = response.getHeaderString("Set-Cookie");
		int m = str.indexOf("=");
		int n = str.indexOf(";");
		sessionId = str.substring(m+1, n);
		System.out.println();
	}
	@Test
	//有效,成功标志="true"
	public void test1() {
		CreateAppTest create = new CreateAppTest();
		DeleteAppTest delete = new DeleteAppTest();
		//新增环境信息
		String response1 = create.createApp(appName,desc,sessionId);
		String appId = response1.substring(response1.indexOf("=")+1,response1.length()-2);
		String response = getEnvById(appId,sessionId);//
		Assert.assertEquals("返回状态比较", response.contains("\"success\":\"true\""), true);
		delete.deleteAppById(appId,sessionId);
	}
	@Test
	//错误信息="400"
	public void test2() {
		String response = getEnvById("sdf",sessionId);//
		Assert.assertEquals("返回状态比较", response.contains("Status 400"), true);
	}
	@Test
	//无效,成功标志="false",错误信息="查找环境信息不存在"
	public void test3() {
		String response = getEnvById("88888888",sessionId);//
		Assert.assertEquals("返回状态比较", response.contains("\"success\":\"false\""), true);
		Assert.assertEquals("返回消息比较", response.contains("\"field\":{\"app\":\"查找应用信息不存在\"}"), true);
	}
	@Test
	////错误信息="404"
	public void test4() {
		String response = getEnvById("",sessionId);//Status 404
		Assert.assertEquals("返回状态比较", response.contains("Status 405"), true);
	}
	
	//指定环境信息获取
	public static String getEnvById(String id,String sessionId){
		Client client = ClientBuilder.newClient();
		StringBuffer url = new StringBuffer();
		url.append(DisconfTestConstant.DISCONF_REST_URI_PREFIX+"/api/appExtension/"+id);
		WebTarget target = client.target(url.toString());
		Response response = target.request().header("EX-JSESSIONID",sessionId).get();
		return response.readEntity(String.class);
	}

}
